extends Node2D
enum SavePathMode{user,exe}
var save_path_mode = SavePathMode.user

func _ready():
	$Add/Game.connect("pressed",$AddPanel,"on_add",["game"])
	$Add/Anime.connect("pressed",$AddPanel,"on_add",["anime"])
	$Add/Book.connect("pressed",$AddPanel,"on_add",["book"])
	$Add/Film.connect("pressed",$AddPanel,"on_add",["film"])
	$Add/Tv.connect("pressed",$AddPanel,"on_add",["tv"])
	$Add/Music.connect("pressed",$AddPanel,"on_add",["music"])
	
	$CollectsVBoxContainer/GameBar/MyGameButton.connect("pressed",$CollectListPanel,"on_show",["game"])
	$CollectsVBoxContainer/AnimeBar/MyAnimeButton.connect("pressed",$CollectListPanel,"on_show",["anime"])
	$CollectsVBoxContainer/BookBar/MyBookButton.connect("pressed",$CollectListPanel,"on_show",["book"])
	$CollectsVBoxContainer/FilmBar/MyFilmButton.connect("pressed",$CollectListPanel,"on_show",["film"])
	$CollectsVBoxContainer/TvBar/MyTvButton.connect("pressed",$CollectListPanel,"on_show",["tv"])
	$CollectsVBoxContainer/MusicBar/MyMusicButton.connect("pressed",$CollectListPanel,"on_show",["music"])
	
	var dataFile = File.new()
	#pc 和安卓的config数据不同，需要分别处理
	if OS.get_name() == "Android":
		$UseExePathCheck.disabled = true
		$TipAndroid.show()
		var AndroidConfigFile = File.new()
		if AndroidConfigFile.file_exists("user://config.txt"):
			AndroidConfigFile.open("user://config.txt",File.READ)
			var mode = AndroidConfigFile.get_as_text()
			if mode == "sdcard":
				$UseSDCardPathCheck.pressed = true
				Global.android_save_mode = Global.AndroidSaveMode.sdcard
				dataFile.open(Global.android_sdcard_path,File.READ)
				Global.dataJson = parse_json(dataFile.get_as_text())
				update_status()
				dataFile.close()
			elif mode == "user":
				$UseSDCardPathCheck.pressed = false
				Global.android_save_mode = Global.AndroidSaveMode.user
				dataFile.open("user://data.json",File.READ)
				Global.dataJson = parse_json(dataFile.get_as_text())
				update_status()
				dataFile.close()
	else:
		$UseSDCardPathCheck.hide()
		$TipAndroid.hide()
		var configFile = File.new()
		if configFile.file_exists("user://config.txt"):
			configFile.open("user://config.txt",File.READ)
			var mode = configFile.get_as_text()
			if mode == "user":
				save_path_mode = SavePathMode.user
				$UseExePathCheck.pressed = false
				Global.save_path_mode = Global.SavePathMode.user
			elif mode == "exe":
				save_path_mode = SavePathMode.exe
				$UseExePathCheck.pressed = true
				Global.save_path_mode = Global.SavePathMode.exe
#		else:
#			configFile.open("user://config.txt",File.WRITE_READ)
#			if save_path_mode == SavePathMode.user:
#				configFile.store_string("user")
#			elif save_path_mode == SavePathMode.exe:
#				configFile.store_string("exe")
		configFile.close()
		if save_path_mode == SavePathMode.user:
			if dataFile.file_exists("user://data.json"):
				dataFile.open("user://data.json",File.READ)
				Global.data = dataFile.get_as_text()
				Global.dataJson = parse_json(Global.data)
				update_status()
			else:
				print_debug("还不存在数据，将会在保存时新建")
		elif save_path_mode == SavePathMode.exe:
			if dataFile.file_exists(OS.get_executable_path().get_base_dir() + "/data.json"):
				dataFile.open(OS.get_executable_path().get_base_dir() + "/data.json",File.READ)
				Global.data = dataFile.get_as_text()
				Global.dataJson = parse_json(Global.data)
				update_status()
			else:
				print_debug("还不存在数据，将会在保存时新建")
	pass
	
# 更新在看 看过 想看 抛弃数据
func update_status():
	var game_watch_count = 0
	var game_want_watch_count = 0
	var game_watched_count = 0
	var game_drop_count = 0
	
	var anime_watch_count = 0
	var anime_want_watch_count = 0
	var anime_watched_count = 0
	var anime_drop_count = 0
	
	var book_watch_count = 0
	var book_want_watch_count = 0
	var book_watched_count = 0
	var book_drop_count = 0
	
	var film_watch_count = 0
	var film_want_watch_count = 0
	var film_watched_count = 0
	var film_drop_count = 0
	
	var tv_watch_count = 0
	var tv_want_watch_count = 0
	var tv_watched_count = 0
	var tv_drop_count = 0
	
	var music_watch_count = 0
	var music_want_watch_count = 0
	var music_watched_count = 0
	var music_drop_count = 0
	
	var game_watch_array = []
	var game_want_watch_array = []
	var game_watched_array = []
	var game_drop_array = []
	
	var anime_watch_array = []
	var anime_want_watch_array = []
	var anime_watched_array = []
	var anime_drop_array = []
	
	var film_watch_array = []
	var film_want_watch_array = []
	var film_watched_array = []
	var film_drop_array = []
	
	var book_watch_array = []
	var book_want_watch_array = []
	var book_watched_array = []
	var book_drop_array = []
	
	var tv_watch_array = []
	var tv_want_watch_array = []
	var tv_watched_array = []
	var tv_drop_array = []
	
	var music_watched_array = []
	var music_watch_array = []
	var music_drop_array = []
	var music_want_watch_array = []
	
	if Global.dataJson != null:
		if Global.dataJson.has("game"):
			for key in Global.dataJson["game"]:
				if Global.dataJson["game"][key]["status"] == "在看":
					game_watch_count +=1
					game_watch_array.append(key)
				elif Global.dataJson["game"][key]["status"] == "想看":
					game_want_watch_count +=1
					game_want_watch_array.append(key)
				elif Global.dataJson["game"][key]["status"] == "看过":
					game_watched_count +=1
					game_watched_array.append(key)
				elif Global.dataJson["game"][key]["status"] == "抛弃":
					game_drop_count +=1
					game_drop_array.append(key)
		if Global.dataJson.has("anime"):
			for key in Global.dataJson["anime"]:
				if Global.dataJson["anime"][key]["status"] == "在看":
					anime_watch_count +=1
					anime_watch_array.append(key)
				elif Global.dataJson["anime"][key]["status"] == "想看":
					anime_want_watch_count +=1
					anime_want_watch_array.append(key)
				elif Global.dataJson["anime"][key]["status"] == "看过":
					anime_watched_count +=1
					anime_watched_array.append(key)
				elif Global.dataJson["anime"][key]["status"] == "抛弃":
					anime_drop_count +=1
					anime_drop_array.append(key)
		if Global.dataJson.has("book"):
			for key in Global.dataJson["book"]:
				if Global.dataJson["book"][key]["status"] == "在看":
					book_watch_count +=1
					book_watch_array.append(key)
				elif Global.dataJson["book"][key]["status"] == "想看":
					book_want_watch_count +=1
					book_want_watch_array.append(key)
				elif Global.dataJson["book"][key]["status"] == "看过":
					book_watched_count +=1
					book_watched_array.append(key)
				elif Global.dataJson["book"][key]["status"] == "抛弃":
					book_drop_count +=1
					book_drop_array.append(key)
		if Global.dataJson.has("film"):
			for key in Global.dataJson["film"]:
				if Global.dataJson["film"][key]["status"] == "在看":
					film_watch_count +=1
					film_watch_array.append(key)
				elif Global.dataJson["film"][key]["status"] == "想看":
					film_want_watch_count +=1
					film_want_watch_array.append(key)
				elif Global.dataJson["film"][key]["status"] == "看过":
					film_watched_count +=1
					film_watched_array.append(key)
				elif Global.dataJson["film"][key]["status"] == "抛弃":
					film_drop_count +=1
					film_drop_array.append(key)
		if Global.dataJson.has("tv"):
			for key in Global.dataJson["tv"]:
				if Global.dataJson["tv"][key]["status"] == "在看":
					tv_watch_count +=1
					tv_watch_array.append(key)
				elif Global.dataJson["tv"][key]["status"] == "想看":
					tv_want_watch_count +=1
					tv_want_watch_array.append(key)
				elif Global.dataJson["tv"][key]["status"] == "看过":
					tv_watched_count +=1
					tv_watched_array.append(key)
				elif Global.dataJson["tv"][key]["status"] == "抛弃":
					tv_drop_count +=1
					tv_drop_array.append(key)
		if Global.dataJson.has("music"):
			for key in Global.dataJson["music"]:
				if Global.dataJson["music"][key]["status"] == "在看":
					music_watch_count +=1
					music_watch_array.append(key)
				elif Global.dataJson["music"][key]["status"] == "想看":
					music_want_watch_count +=1
					music_want_watch_array.append(key)
				elif Global.dataJson["music"][key]["status"] == "看过":
					music_watched_count +=1
					music_watched_array.append(key)
				elif Global.dataJson["music"][key]["status"] == "抛弃":
					music_drop_count +=1
					music_drop_array.append(key)
		$CollectsVBoxContainer/GameBar/WatchButton.text = "在看" + str(game_watch_count)
		$CollectsVBoxContainer/GameBar/WantWatchButton.text = "想看" + str(game_want_watch_count)
		$CollectsVBoxContainer/GameBar/WatchedButton.text = "看过" + str(game_watched_count)
		$CollectsVBoxContainer/GameBar/DropButton.text = "抛弃" + str(game_drop_count)
		
		$CollectsVBoxContainer/AnimeBar/WatchButton.text = "在看" + str(anime_watch_count)
		$CollectsVBoxContainer/AnimeBar/WantWatchButton.text = "想看" + str(anime_want_watch_count)
		$CollectsVBoxContainer/AnimeBar/WatchedButton.text = "看过" + str(anime_watched_count)
		$CollectsVBoxContainer/AnimeBar/DropButton.text = "抛弃" + str(anime_drop_count)
		
		$CollectsVBoxContainer/BookBar/WatchButton.text = "在看" + str(book_watch_count)
		$CollectsVBoxContainer/BookBar/WantWatchButton.text = "想看" + str(book_want_watch_count)
		$CollectsVBoxContainer/BookBar/WatchedButton.text = "看过" + str(book_watched_count)
		$CollectsVBoxContainer/BookBar/DropButton.text = "抛弃" + str(book_drop_count)
		
		$CollectsVBoxContainer/FilmBar/WatchButton.text = "在看" + str(film_watch_count)
		$CollectsVBoxContainer/FilmBar/WantWatchButton.text = "想看" + str(film_want_watch_count)
		$CollectsVBoxContainer/FilmBar/WatchedButton.text = "看过" + str(film_watched_count)
		$CollectsVBoxContainer/FilmBar/DropButton.text = "抛弃" + str(film_drop_count)
		
		$CollectsVBoxContainer/TvBar/WatchButton.text = "在看" + str(tv_watch_count)
		$CollectsVBoxContainer/TvBar/WantWatchButton.text = "想看" + str(tv_want_watch_count)
		$CollectsVBoxContainer/TvBar/WatchedButton.text = "看过" + str(tv_watched_count)
		$CollectsVBoxContainer/TvBar/DropButton.text = "抛弃" + str(tv_drop_count)
		
		$CollectsVBoxContainer/MusicBar/WatchButton.text = "在看" + str(music_watch_count)
		$CollectsVBoxContainer/MusicBar/WantWatchButton.text = "想看" + str(music_want_watch_count)
		$CollectsVBoxContainer/MusicBar/WatchedButton.text = "看过" + str(music_watched_count)
		$CollectsVBoxContainer/MusicBar/DropButton.text = "抛弃" + str(music_drop_count)
		
		$CollectsVBoxContainer/GameBar/WatchButton.connect("pressed",$CollectListPanel,"on_status_show",["game",game_watch_array])
		$CollectsVBoxContainer/GameBar/WantWatchButton.connect("pressed",$CollectListPanel,"on_status_show",["game",game_want_watch_array])
		$CollectsVBoxContainer/GameBar/WatchedButton.connect("pressed",$CollectListPanel,"on_status_show",["game",game_watched_array])
		$CollectsVBoxContainer/GameBar/DropButton.connect("pressed",$CollectListPanel,"on_status_show",["game",game_drop_array])
		
		$CollectsVBoxContainer/AnimeBar/WatchButton.connect("pressed",$CollectListPanel,"on_status_show",["anime",anime_watch_array])
		$CollectsVBoxContainer/AnimeBar/WantWatchButton.connect("pressed",$CollectListPanel,"on_status_show",["anime",anime_want_watch_array])
		$CollectsVBoxContainer/AnimeBar/WatchedButton.connect("pressed",$CollectListPanel,"on_status_show",["anime",anime_watched_array])
		$CollectsVBoxContainer/AnimeBar/DropButton.connect("pressed",$CollectListPanel,"on_status_show",["anime",anime_drop_array])
		
		$CollectsVBoxContainer/BookBar/WatchButton.connect("pressed",$CollectListPanel,"on_status_show",["book",book_watch_array])
		$CollectsVBoxContainer/BookBar/WantWatchButton.connect("pressed",$CollectListPanel,"on_status_show",["book",book_want_watch_array])
		$CollectsVBoxContainer/BookBar/WatchedButton.connect("pressed",$CollectListPanel,"on_status_show",["book",book_watched_array])
		$CollectsVBoxContainer/BookBar/DropButton.connect("pressed",$CollectListPanel,"on_status_show",["book",book_drop_array])
		
		$CollectsVBoxContainer/FilmBar/WatchButton.connect("pressed",$CollectListPanel,"on_status_show",["film",film_watch_array])
		$CollectsVBoxContainer/FilmBar/WantWatchButton.connect("pressed",$CollectListPanel,"on_status_show",["film",film_want_watch_array])
		$CollectsVBoxContainer/FilmBar/WatchedButton.connect("pressed",$CollectListPanel,"on_status_show",["film",film_watched_array])
		$CollectsVBoxContainer/FilmBar/DropButton.connect("pressed",$CollectListPanel,"on_status_show",["film",film_drop_array])
		
		$CollectsVBoxContainer/TvBar/WatchButton.connect("pressed",$CollectListPanel,"on_status_show",["tv",tv_watch_array])
		$CollectsVBoxContainer/TvBar/WantWatchButton.connect("pressed",$CollectListPanel,"on_status_show",["tv",tv_want_watch_array])
		$CollectsVBoxContainer/TvBar/WatchedButton.connect("pressed",$CollectListPanel,"on_status_show",["tv",tv_watched_array])
		$CollectsVBoxContainer/TvBar/DropButton.connect("pressed",$CollectListPanel,"on_status_show",["tv",tv_drop_array])
		
		$CollectsVBoxContainer/MusicBar/WatchButton.connect("pressed",$CollectListPanel,"on_status_show",["music",music_watch_array])
		$CollectsVBoxContainer/MusicBar/WantWatchButton.connect("pressed",$CollectListPanel,"on_status_show",["music",music_want_watch_array])
		$CollectsVBoxContainer/MusicBar/WatchedButton.connect("pressed",$CollectListPanel,"on_status_show",["music",music_watched_array])
		$CollectsVBoxContainer/MusicBar/DropButton.connect("pressed",$CollectListPanel,"on_status_show",["music",music_drop_array])
	pass
	
func _on_openDataDirButton_pressed():
	if save_path_mode == SavePathMode.user:
		OS.shell_open(ProjectSettings.globalize_path("user://"))
	elif save_path_mode == SavePathMode.exe:
		OS.shell_open(OS.get_executable_path().get_base_dir())
	pass


func _on_UseExePathCheck_toggled(button_pressed):
	var file2 = File.new()
	file2.open("user://config.txt",File.WRITE_READ)
	if button_pressed:
		save_path_mode = SavePathMode.exe
		Global.save_path_mode = Global.SavePathMode.exe
		file2.store_string("exe")
	else:
		save_path_mode = SavePathMode.user
		Global.save_path_mode = Global.SavePathMode.user
		file2.store_string("user")
	file2.close()
	pass # Replace with function body.


func _on_Refresh_pressed():
	get_tree().reload_current_scene()
	pass # Replace with function body.


func _on_CopyDataButton_pressed():
	OS.clipboard = to_json(Global.dataJson)
	pass # Replace with function body.


func _on_UseSDCardPathCheck_toggled(button_pressed):
	var AndroidConfigFile = File.new()
	AndroidConfigFile.open("user://config.txt",File.WRITE_READ)
	if button_pressed:
		Global.android_save_mode = Global.AndroidSaveMode.sdcard
		AndroidConfigFile.store_string("sdcard")
	else:
		Global.android_save_mode = Global.AndroidSaveMode.user
		AndroidConfigFile.store_string("user")
	pass # Replace with function body.
